//
//  AppDelegate.swift
//  HYYWeibo
//
//  Created by HY-Mac on 16/7/8.
//  Copyright © 2016年 HY-Mac. All rights reserved.
//

import UIKit
import QorumLogs

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
       // QorumLogs.enabled = true
       // QorumLogs.test()
       // HYLog(123)
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.rootViewController = defaultViewController()
        window?.backgroundColor = UIColor.whiteColor()
        window?.makeKeyAndVisible()
        
        setThemeColor()
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AppDelegate.ChangeRootViewController(_:)), name: HYChangeRootViewController, object: nil)
        
        HYLog(isNewVersion())
        
        return true
    }


}
private func setThemeColor() {
    UINavigationBar.appearance().tintColor = themeColor
    UITabBar.appearance().tintColor = themeColor
}

func HYLog<T>(message: T, fileName: String = #file, methodName: String = #function, lineNum: Int = #line){
  //  let name = (fileName as NSString).pathComponents.last!
    #if DEBUG
    print("\(methodName).[\(lineNum)]:\(message)")
    #endif
    
}
extension AppDelegate{
    
    private func defaultViewController() -> UIViewController{
        if !AccountViewModel().userLogin{
            let sb = UIStoryboard(name: "Main", bundle: nil)
            return sb.instantiateInitialViewController()!
        }else{
            if isNewVersion(){
                let sb = UIStoryboard(name: "Newfeature", bundle: nil)
                return sb.instantiateInitialViewController()!
            }else{
                let sb = UIStoryboard(name: "Welcome", bundle: nil)
                return sb.instantiateInitialViewController()!
            }
        }
    }
    
    func ChangeRootViewController(notice: NSNotification){
        let flag = notice.object as? Bool
        if (flag != false){
            let sb = UIStoryboard(name: "Main", bundle: nil)
            window?.rootViewController = sb.instantiateInitialViewController()!
        }else{
            let sb = UIStoryboard(name: "Welcome", bundle: nil)
            window?.rootViewController = sb.instantiateInitialViewController()!
        }
    }
    
    private func isNewVersion() -> Bool
    {
        // 1.加载info.plist
        // 2.获取当前软件的版本号
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        // 3.获取以前的软件版本号?
        let defaults = NSUserDefaults.standardUserDefaults()
        let sanboxVersion = (defaults.objectForKey("xxoo") as? String) ?? "0.0"
        // 4.用当前的版本号和以前的版本号进行比较
        // 1.0  0.0
        if currentVersion.compare(sanboxVersion) == NSComparisonResult.OrderedDescending
        {
            // 如果当前的大于以前的, 有新版本
            HYLog("有新版本")
            // 如果有新版本, 就利用新版本的版本号更新本地的版本号
            
            defaults.setObject(currentVersion, forKey: "xxoo")
            defaults.synchronize() // iOS7以前需要写, iOS7以后不用写
            return true
        }
        HYLog("没有新版本")
        return false
    }
    
    
}


